/*
 * Scaffold Application Framework
 *
 * Copyright (c) 2010 Sii Sp. z o.o. All rights reserved.
 */
package com.gdziarmaga.webshop.web.managedBeans.book;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.gdziarmaga.webshop.book.model.Book;
import com.gdziarmaga.webshop.book.services.BookManager;
import com.gdziarmaga.webshop.web.datatable.BaseTableDataModelImpl;
import com.gdziarmaga.webshop.web.datatable.BaseTableRow;
import com.gdziarmaga.webshop.web.utils.FacesUtils;



@ManagedBean(name="bookTableDataModel")
@Scope("request")
public class BookTableDataModel extends BaseTableDataModelImpl<Book> {
    /**
     * The serialVersionUID.
     */
    private static final long serialVersionUID = 6656731704264566928L;
    /**
     * Book Manager.
     */
    @Autowired
    private BookManager bookManager;
    /**
     * Current Book - set manually by programmer.
     */
    private Book currentBook;

    /**
     * {@inheritDoc}
     */
    @Override
    @PostConstruct
    public void init() {
        super.init();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getObjectId(Book object) {
        return object.getBook_id();
    }

    /**
     * Applies the filter to this bean.
     */
    public void applyFilter() {
        setFilter((BookFilterBean) FacesUtils.getRequest().getAttribute("bookFilterBean"));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<BaseTableRow<Book>> getObjectList() {
        List<BaseTableRow<Book>> result = new ArrayList<BaseTableRow<Book>>();
        List<Book> temp = bookManager.getBooksByFilterObject(getFilter());
        if (temp != null && !temp.isEmpty()) {
            for (Book Book : temp) {
                result.add(new BookTableRow(Book));
            }
        }
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getTableRowCount() {
//        return BookManager.getBookCount(getFilter());
    	return 27;
    }

    /**
     * Navigation method used to navigate to
     * <tt>/protected/Books/Book.xhtml</tt>
     * 
     * @return the navigation {@link String}
     * @throws IOException
     * @throws ServletException
     */
//    public void goToBook() throws ServletException, IOException {
//        String redirectUrl = "/protected/Books/Book.xhtml";
//        FacesUtils.putRequestSessionAttribute(redirectUrl, "BookFilterBean", FacesUtils.getRequestAttribute("BookFilterBean"));
//        FacesUtils.putRequestSessionAttribute(redirectUrl, "BookTableDataModel", FacesUtils.getRequestAttribute("BookTableDataModel"));
//        FacesUtils.putRequestSessionAttribute(redirectUrl, Parameters.PARAM_RECORD_ID, FacesUtils.getRequestParameter(Parameters.PARAM_RECORD_ID));
//        FacesUtils.redirect(redirectUrl);
//    }

    /**
     * Getter for currentBook.
     * 
     * @return the currentBook
     */
    public Book getCurrentBook() {
        return currentBook;
    }

    /**
     * Setter for currentBook.
     * 
     * @param currentBook the currentBook to set
     */
    public void setCurrentBook(Book currentBook) {
        this.currentBook = currentBook;
    }
}
